/* import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex) */

import { createStore } from 'vuex' 

const store = createStore({
	state: {
		islogin: false,
		userInfo: {},
		token: '',
		token_expired: 0,

		__key_user_info: 'user_info',
		__key_token: 'uni_id_token',
		__key_token_expired: 'uni_id_token_expired',
	},
	mutations: {
		// 登录
		login(state, res) {
			state.islogin = true;

			store.commit('setToken', res);
			store.commit('setUserInfo', res);
		},
		// 设置 token
		setToken(state, res) {
			state.token = res.token;
			state.token_expired = res.tokenExpired;

			uni.setStorage({
				key: state.__key_token,
				data: state.token
			})

			uni.setStorage({
				key: state.__key_token_expired,
				data: state.token_expired
			})
		},
		// 设置 userInfo
		setUserInfo(state, res) {
			state.userInfo = res.userInfo;

			uni.setStorage({
				key: state.__key_user_info,
				data: state.userInfo
			})
		},
		// 加载 Token
		loadToken(state) {
			state.userInfo = uni.getStorageSync(state.__key_user_info);
			state.token = uni.getStorageSync(state.__key_token);
			state.token_expired = uni.getStorageSync(state.__key_token_expired);

			if (!!state.token && state.token_expired > Date.now()) {
				state.islogin = true;
			}
		},
		// 登出
		logout(state) {
			state.islogin = false;
			state.userInfo = {};
			state.token = '';
			state.token_expired = 0;

			uni.removeStorage({
				key: state.__key_user_info
			})
			uni.removeStorage({
				key: state.__key_token
			})
			uni.removeStorage({
				key: state.__key_token_expired
			})

			for (let key in localStorage) {
				if (key.indexOf('__scroll') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__search') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__cutSta') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('usemall-tabs') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__tabs') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__paginationPage') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__paginationRows') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('hintTime') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__orderCutSta') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__visitCutSta') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__browseCutSta') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__memberCutSta') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('__refundCutSta') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('version') != -1) {
					uni.removeStorageSync(key);
				}
				if (key.indexOf('isWxExpress') != -1) {
					uni.removeStorageSync(key);
				}
			}
		}
	}
})

export default store
